<?php
	class Sys_user extends MY_controller{

		function __construct(){
			parent::__construct();
			$this->load->model('sys_users');
		}

		/**
		* 函数功能介绍：返回后台用户列表
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-25 09:29
		* 最后修改人：
		* 最后修改时间：
		*/

		function index($search = 0,$limit = 0){
			if($this->input->post()){
				$search = $this->input->post('search',TRUE) ? urldecode($this->input->post('search',TRUE)) : 0;
				$limit = 0;
			}
			$search = urldecode($search);
			$data = array('search' => $search,'limit' => $limit);
			$this->smarty->assign("action", '后台用户列表');
			$offset = 10;
			$result = $this->sys_users->get_manage_user_list($data['limit'], $offset, $data);
			$this->load->library('pagination');
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$page_config['base_url'] = '/sys_user/index/'.$data['search'];
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $offset; 
			$page_config['uri_segment'] = 4;
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("data", $data);
			$this->smarty->assign("manage_user_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("sys_user/list.html");
		}

		/**
		* 函数功能介绍：添加后台用户
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-22 17:54
		* 最后修改人：
		* 最后修改时间：
		*/

		function add(){
			$this->load->model("roles");
			$role_list = $this->roles->get_all_role_list();
			$this->smarty->assign("role_list", $role_list);
			$this->smarty->assign("action", '添加后台用户');
			$this->smarty->display("sys_user/save.html");
		}

		/**
		* 函数功能介绍：编辑后台用户
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-25 09:55
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		*/

		function edit($id){
			$this->load->model("roles");
			$role_list = $this->roles->get_all_role_list();
			$this->smarty->assign("role_list", $role_list);
			$manage_user_info = get_field_by_where('sys_users', '*', "id='{$id}'");
			$this->smarty->assign("action", '编辑后台用户');
			$this->smarty->assign("manage_user_info", $manage_user_info);
			$this->smarty->display("sys_user/save.html");
		}

		/**
		* 函数功能介绍：保存数据
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-22 18:01
		* 最后修改人：
		* 最后修改时间：
		*/

		function save(){
			if( ! $this->input->post()) {
				show_message('/sys_user', "非法操作！");exit;
			}
			$data = $this->input->post();
			if($data['id'] > 0){
				$this->update_manage_user($data);
			}else{
				$this->add_manage_user($data);
			}
		}

		/**
		* 添加后台用户数据
		* Create: 	Zhou Hailong
		* Cretaed: 2014-08-22 18:27
		* 最后修改人：
		* Last update：
		*/

		function add_manage_user($data){
			if($data['password'] != $data['confirm_password']) {
				show_message('/sys_user/add', '您两次输入的密码不一一致,请重新输入!');
			}else{
				unset($data['confirm_password']);
			}
			$md5_pass = md5($data['password']);
			$rand_str = '';
			for($i=0; $i<10; $i++){
				$rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
			}
			$data['rand_str'] = $rand_str;
			$data['password'] = md5($md5_pass.$rand_str);
			$result = $this->sys_users->insert($data);
			if($result == TRUE){
				show_message('/sys_user', "用户添加成功");
			}else{
				show_message('/sys_user', "用户添加失败，请重试！");
			}
		}

		/**
		* 修改角色数据
		* Create: 	Zhou Hailong
		* Cretaed: 2014-08-25 15:09
		* 最后修改人：
		* Last update：
		*/
		function update_manage_user($data){
			$this->db->where('id', $data['id']);
 			$this->db->update('sys_users', $data);
 			show_message($_SERVER['HTTP_REFERER'], "用户信息修改成功");
		}


/**
		* 修改用户密码
		* Create: 	Yang Ningshu
		* Cretaed: 2014-09-10 9:55
		* 最后修改人：
		* Last update：
		*/
		function ignore_update_manage_user_password(){
			if($this->input->post()){
				$this->load->model('logins');
				$uid = $this->session->userdata['manage_user']['uid'];
				$rand_str = $this->sys_users->get_password_rand_str();
				$old_password = md5(md5($this->input->post('old_password')) . $rand_str['rand_str']);
				$result = $this->logins->chaeck_password($uid,$old_password);
				if($result){
					if($this->input->post('password') == $this->input->post('confirm_password')){
						$new_password = md5(md5($this->input->post('password')) . $rand_str['rand_str']);
						$this->db->where('id', $uid);
						$data['edit_time'] = time();
						$data['password'] = $new_password;
 						$results = $this->db->update('manage_user', $data);
 						if($results){
 							$this->session->unset_userdata('manage_user');
 							show_message('/login', "密码修改成功，请重新登录！");
							exit;
 						}else{
 							show_message('/login', "密码修改失败，请联系开发人员！");
							exit;
 						}
					}
				}else{
					show_message('/sys_user/ignore_update_manage_user_password', "旧密码输入错误！");
					exit;
				}
			}
			$this->smarty->assign("action", '用户修改密码');
			$this->smarty->display("update_manage_user_password.html");
		}



		/**
		* 函数功能介绍：删除后台用户
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-25 10:11
		* 最后修改人：
		* 最后修改时间：
		* @param ids int
		*/
		function delete($id = 0){
			if($id){
				$result = $this->sys_users->delete($id);
				if($result == TRUE){
					show_message('/sys_user', "用户信息删除成功");
				}else{
					show_message('/sys_user', "用户信息删除失败，请重试！");
				}
			}else{
				$ids = $this->input->post('ids');
				if(count($ids) > 0){
					$this->sys_users->batch_delete($ids);
					show_message($_SERVER['HTTP_REFERER'], "用户信息删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "您没有选中任何数据");exit;
				}
			}
		}

		/**
		* 函数功能介绍：会员启用禁用
		* 创建人: 	Yang Ningshu
		* 创建时间: 2014-09-04 11:29
		* 最后修改人：
		* 最后修改时间：
		*/
		function status() {
			$return = array();
			if( !$this->input->get() || !$this->input->get('id')) {
				$return = array('code'=>9,'msg'=>'非法操作！');
			}else {
				$result = $this->sys_users->update_user_status($this->input->get('id'),array('status'=>$this->input->get('status')));
				if($result) {
					$return = array('code'=>0,'msg'=>'成功修改');
				}else {
					$return = array('code'=>1,'msg'=>'修改失败');
				}
			}
			echo json_encode($return);exit;
		}

		/**
		* 函数功能介绍：查看角色详情
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-22 11:12
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		*/
		function view($id){
			$this->load->model("roles");
			$role_list = $this->roles->get_all_role_list();
			$this->smarty->assign("role_list", $role_list);
			$manage_user_info = $this->sys_users->get_manage_user_info($id);
			$this->smarty->assign("action", '查看后台用户');
			$this->smarty->assign("manage_user_info", $manage_user_info);
			$this->smarty->display("sys_user/view.html");
		}

		function update_admin_pwd(){
			if($this->input->post()){
				$this->load->model('logins');
				$uid = $this->session->userdata['manage_user']['uid'];
				$rand_str = $this->sys_users->get_password_rand_str();
				$old_password = md5(md5($this->input->post('old_password')) . $rand_str['rand_str']);
				$result = $this->logins->chaeck_password($uid,$old_password);
				if($result){
					if($this->input->post('password') == $this->input->post('confirm_password')){
						$new_password = md5(md5($this->input->post('password')) . $rand_str['rand_str']);
						$this->db->where('id', $uid);
						$data['password'] = $new_password;
 						$results = $this->db->update('sys_users', $data);
 						if($results){
 							$this->session->unset_userdata('manage_user');
 							show_message('/login', "密码修改成功，请重新登录！");
							exit;
 						}else{
 							show_message('/login', "密码修改失败，请联系开发人员！");
							exit;
 						}
					}
				}else{
					show_message('/sys_user/update_admin_pwd', "旧密码输入错误！");
					exit;
				}
			}else{
				$this->smarty->assign("action", '修改密码');
				$this->smarty->display("sys_user/update_admin_pwd.html");
			}
		}

		function update_school_pwd(){
			if($this->input->post()){
				$this->load->model('logins');
				$post_data = $this->input->post();
				$uid = $this->input->post('uid');
				if($this->input->post('password') == $this->input->post('confirm_password')){
					$rand_str = $this->sys_users->get_user_password_rand_str($uid);
					$new_password = md5(md5($post_data['password']) . $rand_str['rand_str']);
					$this->db->where('id', $uid);
					$data['password'] = $new_password;
					$results = $this->db->update('user', $data);
					show_message($_SERVER['HTTP_REFERER'], "密码修改成功");
				}
			}else{
				$this->smarty->assign("action", '修改密码');
				$this->smarty->display("sys_user/update_admin_pwd.html");
			}
		}

		function chage_school_pwd($school_id = 0){
			if($school_id <= 0){
				show_message("缺少学校ID");exit;
			}
			$school_manage = get_field_by_where("o2o_teachers", "user_id", "school_id='{$school_id}' AND is_manager = 1");
			if(empty($school_manage['user_id'])){
				show_message("该学校的管理员账号不存在或已被禁用");exit;
			}
			$this->smarty->assign("user_id", $school_manage['user_id']);
			$this->smarty->assign("action", '修改密码');
			$this->smarty->display("sys_user/update_admin_pwd.html");
		}
	}